<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\UserDraw;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Admin\Renderable\UserTable;
use App\Models\User;
use App\Admin\Renderable\ActivityTable;
use App\Models\Activity;

class UserDrawController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $model = UserDraw::with(['activity']);

        return Grid::make($model, function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('user_id')->modal('用户详情', function () {
                return app('App\Admin\Controllers\UserController')->current($this->user_id);
            });
            $grid->column('activity.title', '活动')->label();
            $grid->column('customer_info_id');
            $grid->column('draw_at');
            $grid->column('prize', '奖品')->display(function ($prize) {
                return is_null($prize) ? '' : $prize['name'];
            });
            $grid->column('address_at');
            $grid->column('address', '收货地址')->display(function ($address) {
                return is_null($address) ? '' : $address['contact_name'] . ' ' . $address['contact_phone'] . ' ' . $address['contact_address'];
            });
            $grid->column('reason');
            $grid->column('created_at');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->equal('id')->integer()->width(2);
                $filter->equal('customer_info_id', '留资ID')->integer()->width(2);
                $filter->equal('user_id', '用户')->width(2)
                    ->selectTable(UserTable::make(['id' => ''])) // 设置渲染类实例，并传递自定义参数
                    ->title('用户列表')
                    ->dialogWidth('50%') // 弹窗宽度，默认 800px
                    ->model(User::class, 'id', 'name'); // 设置编辑数据显示

                $filter->equal('activity_id', '活动')->width(3)
                    ->selectTable(ActivityTable::make(['id' => '']))
                    ->title('活动列表')
                    ->dialogWidth('50%')
                    ->model(Activity::class, 'id', 'title');
                $filter->where('win', function ($query) {
                    $win = $this->input;
                    if ($win) {
                        return $query->whereNotNull('prize');
                    } else {
                        return $query->whereNull('prize');
                    }
                }, '是否中奖')->select([1 => '是', 0 => '否'])->width(3);
                $filter->between('created_at')->datetime()->width(4);
            });
            $grid->disableCreateButton();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->export()->rows(function ($rows) {
                foreach ($rows as $index => &$row) {
                    $row['prize'] = is_null($row['prize']) ? '' : $row['prize']['name'];
                    $row['address'] = is_null($row['address']) ? '' : $row['address']['contact_name'] . ' ' . $row['address']['contact_phone'] . ' ' . $row['address']['contact_address'];
                }

                return $rows;
            })->csv();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new UserDraw(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('activity_id');
            $show->field('customer_info_id');
            $show->field('draw_at');
            // $show->field('prize');
            // $show->field('address');
            $show->field('address_at');
            $show->field('reason');
            $show->field('created_at');
            $show->field('updated_at');
            $show->panel()
                ->tools(function ($tools) {
                    $tools->disableEdit();
                    $tools->disableDelete();
                });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new UserDraw(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('activity_id');
            $form->text('customer_info_id');
            $form->text('draw_at');
            $form->text('prize');
            $form->text('address');
            $form->text('address_at');
            $form->text('reason');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
